<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head>
    <title>Title</title>
    <link rel="stylesheet" href="/static/layui/css/layui.css">
</head>
<body>
<form class="layui-form layui-form-pane" lay-filter="formFilter" action="">
    <input type="hidden" name="id">
    <div class="layui-form-item">
        <label class="layui-form-label">商品名</label>
        <div class="layui-input-block">
            <input type="text" name="name" autocomplete="off" placeholder="请输入" lay-verify="required"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">子标题</label>
        <div class="layui-input-block">
            <input type="text" name="subtitle" autocomplete="off" placeholder="请输入" lay-verify="required"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">一级分类</label>
        <div class="layui-input-inline">
            <select id="topCategory" name="topCategoryId" lay-filter="topCategory">
                <option value="">---请选择---</option>
            </select>
        </div>
        <label class="layui-form-label">一级分类</label>
        <div class="layui-input-inline">
            <select id="categoryId" name="categoryId" lay-filter="aihao">
                <option value="">---请选择---</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">价格</label>
        <div class="layui-input-block">
            <input type="text" name="price" autocomplete="off" placeholder="请输入" lay-verify="required"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">库存</label>
        <div class="layui-input-block">
            <input type="text" name="stock" autocomplete="off" placeholder="请输入" lay-verify="required"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">图片</label>
        <div class="layui-input-block">
            <div class="layui-upload-list">
                <input type="hidden" name="mainImage" id="imageId">
                <img class="layui-upload-img" id="ID-upload-demo-img" width="150px" height="150px">
                <div id="ID-upload-demo-text"></div>
            </div>
            <button type="button" class="layui-btn" id="uploadId">
                <i class="layui-icon layui-icon-upload"></i> 单图片上传
            </button>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">商品详情</label>
        <div class="layui-input-block">
            <textarea id="detailId"  name="detail" placeholder="请输入内容" class="layui-textarea"></textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit lay-filter="submitForm">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<script src="/static/jquery-2.1.4.js" type="text/javascript" charset="utf-8"></script>
<script src="/static/layui/layui.js" type="text/javascript" charset="utf-8"></script>
<script src="/static/mylayer.js" type="text/javascript" charset="utf-8"></script>
<script src="/static/kindeditor/kindeditor.js" type="text/javascript" charset="utf-8"></script>
<script>
    layui.use(['form', 'upload'], function () {
        var form = layui.form;
        var upload = layui.upload;
        var layer = layui.layer;

        var kindEditorParams = {
            afterBlur: function() {
                this.sync();
            }
        };
        var editor = KindEditor.create('#detailId', kindEditorParams);

        var queryString = window.location.search;
        // ?id=1
        console.log('queryString: ' + queryString);
        var urlParams = new URLSearchParams(queryString);
        var id = urlParams.get("id");

        $.post(
            '/product/selectById',
            {'id': id},
            function (result) {
                console.log(result.data);
                if (result.code == 0) {
                    var topCategoryList = result.data.topCategoryList;
                    $(topCategoryList).each(function () {
                        $('#topCategory').append('<option value="'+this.id+'">'+this.name+'</option>');
                    })
                    var secondCategoryList = result.data.secondCategoryList;
                    $(secondCategoryList).each(function () {
                        $('#categoryId').append('<option value="'+this.id+'">'+this.name+'</option>');
                    })
                    form.render('select');

                    var product = result.data.product;
                    product.topCategoryId = result.data.topCategoryId;
                    form.val('formFilter', product);
                    $('#ID-upload-demo-img').attr('src', product.mainImage);

                    editor.html(product.detail);
                }
            },
            'json'
        );

        form.on('select(topCategory)', function(data){
            var elem = data.elem; // 获得 select 原始 DOM 对象
            var value = data.value; // 获得被选中的值
            var othis = data.othis; // 获得 select 元素被替换后的 jQuery 对象

            $.post(
                '/category/selectSecondCategoryByTopCategoryId',
                {'id': value},
                function (result) {
                    console.log(result);
                    if (result.code == 0) {
                        $('#categoryId option:gt(0)').remove();
                        $(result.data).each(function () {
                            $('#categoryId').append('<option value="'+this.id+'">'+this.name+'</option>');
                        })
                        form.render('select');
                    }
                },
                'json'
            );
        });


        // 单图片上传
        var uploadInst = upload.render({
            elem: '#uploadId',
            url: '/upload', // 实际使用时改成您自己的上传接口即可。
            before: function(obj){
                // 预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#ID-upload-demo-img').attr('src', result); // 图片链接（base64）
                });
            },
            done: function(result){
                if (result.code == 0) {
                    console.log(result);
                    $('#imageId').val(result.data);
                } else {
                    mylayer.errorMsg('上传失败')
                }
                $('#ID-upload-demo-text').html('');
            },
            error: function(){
                // 演示失败状态，并实现重传
                var demoText = $('#ID-upload-demo-text');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            },
        });


        // 提交事件 layui
        form.on('submit(submitForm)', function (data) {
            var field = data.field; // 获取表单字段值
            //{"name":"UI","credit":"12"}
            // 此处可执行 Ajax 等操作
            $.post(
                '/product/update',
                data.field,
                function (result) {
                    console.log(result);
                    if (result.code == 0) {
                        mylayer.okMsg(result.msg);
                        //table.reload('tableId');
                        setInterval(function () {
                            //关闭弹出层
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                            //刷新父页面
                            window.parent.location.reload();
                        }, 2000);
                    }
                },
                'json'
            );

            return false; // 阻止默认 form 跳转
        });
    });
</script>
</body>
</html>